﻿Namespace BL


    Public Module Inventory

#Region "Inventory_DML"

        Public Function InsertInventory(dict As Dictionary(Of String, Object)) As Integer
            Return DDA.Insert("Inventory", dict)
        End Function

        Public Function UpdateInventory(dict As Dictionary(Of String, Object), InventoryID As Integer) As Integer
            Return DDA.Update("Inventory", dict, "InventoryID=" & InventoryID)
        End Function

        Public Function DeleteInventory(ByVal InventoryID As Integer) As Integer
            Return DDA.Delete("Inventory", "InventoryID=" & InventoryID, False)
        End Function

#End Region

#Region "Inventory_Selection"

        Public Function GetAllInventoryByProductID(ProductId As Integer) As IDataReader
            Dim sql As String = "SELECT * FROM Inventory WHERE ProductID = " & ProductId & " Order By InventoryID DESC"
            Return DDA.SelectData(sql)
        End Function

        Public Function GetStock(searchFor As String, criteria As String, startRowIndex As Integer, maximumRows As Integer, SortBy As String) As IDataReader
            Dim sql As String
            sql = "SELECT Brands.BrandName,Categories.CategoryName, Products.ProductName,Products.ProductID, SUM(ISNULL(Inventory.QUantity,0)) As Stock" & _
                    " FROM Products LEFT JOIN Brands ON Products.BrandID = Brands.BrandID LEFT JOIN Categories ON Products.CategoryID = Categories.CategoryID " & _
                    " LEFT JOIN Inventory ON Products.ProductID = Inventory.ProductID GROUP BY Brands.BrandName,Categories.CategoryName,Products.ProductName,Products.ProductID "

            If SortBy = "" Then SortBy = " BrandName Asc"

            Return DDA.SelectDataPaginated(sql, startRowIndex, maximumRows, SortBy)
        End Function

        Public Function GetStockCount(searchFor As String, criteria As String) As Integer
            Dim sql As String
            sql = "SELECT Brands.BrandName,Categories.CategoryName, Products.ProductName,Products.ProductID, SUM(ISNULL(Inventory.QUantity,0)) As Stock" & _
                    " FROM Products LEFT JOIN Brands ON Products.BrandID = Brands.BrandID LEFT JOIN Categories ON Products.CategoryID = Categories.CategoryID " & _
                    " LEFT JOIN Inventory ON Products.ProductID = Inventory.ProductID GROUP BY Brands.BrandName,Categories.CategoryName,Products.ProductName,Products.ProductID "

            Return DDA.SelectDataCount(sql)
        End Function

#End Region

    End Module

End Namespace
